using System.Collections;
using System.Linq;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Params;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Mvc;

namespace ArticleManagementSystem.Api.Controllers
{

    [ApiController]
    [Route("[controller]")]
    public class PageviwesController : ControllerBase
    {
        private readonly IRepostisory<Pageviwes> _pageviwesRepostisory;
        private readonly IRepostisory<Article> _articleRepostisory;

        public PageviwesController(IRepostisory<Article> articleRepostisory, IRepostisory<Pageviwes> pageviwesRepostisory)
        {
            _pageviwesRepostisory = pageviwesRepostisory;
            _articleRepostisory = articleRepostisory;
        }


        /// <summary>
        /// 获取浏览量表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetPageviews()
        {
            var page = _pageviwesRepostisory.Table.GroupBy(p => new { p.ArticleId })
                    .Select(p => new { ArticleId = p.Key.ArticleId, count = p.Sum(s => 1) })
                    .OrderByDescending(x => x.count).ToList();
            var article = _articleRepostisory.Table;
            ArrayList arrlist = new ArrayList();
            foreach (var item in page)
            {
                var articleMsg = article.Where(x => x.Id == item.ArticleId).FirstOrDefault();
                if (articleMsg != null)
                {
                    arrlist.Add(articleMsg);
                }

            }
            return new
            {
                code = 200,
                data = arrlist,
                count = page,
                msg = "请求成功"
            };
        }


        /// <summary>
        /// 添加浏览量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public dynamic PostPageviews(GetLike getLike)
        {
            var userId = getLike.UserId;
            var articleId = getLike.ArticleId;
            var page = _pageviwesRepostisory.Table;
            if (userId > 0 && articleId > 0)
            {

                var addPageView = new Pageviwes
                {
                    UserId = userId,
                    ArticleId = articleId
                };

                _pageviwesRepostisory.Insert(addPageView);

                return new
                {
                    code = 200,
                    data = page,
                    msg = "浏览量 + 1"
                };
            }
            else if (userId < 0 && articleId > 0)
            {
                var addPageView = new Pageviwes
                {
                    UserId = 0,
                    ArticleId = articleId
                };
                _pageviwesRepostisory.Insert(addPageView);
                return new
                {
                    code = 200,
                    data = page,
                    msg = "游客状态添加浏览量"
                };
            }
            else if (userId > 0 && articleId < 0)
            {
                return new
                {
                    code = 404,
                    data = "",
                    msg = "!!!"
                };
            }
            else if (userId < 0 && articleId < 0)
            {
                return new
                {
                    code = 404,
                    data = "",
                    msg = "!!!!!"
                };
            }

            return new
            {
                code = 1000,
                data = "",
                msg = "！！！！"
            };
        }
    }
}